package com.example.document

import com.example.lib_base.BaseRepo

import com.example.lib_base.Res
import com.example.lib_http.Const
import com.example.lib_http.HttpType
import com.example.lib_http.NetworkFactory
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import javax.inject.Inject

class DocumentRepo @Inject constructor() : BaseRepo() {
    private val apiService by lazy {
        NetworkFactory.factory(HttpType.NONE).create(ApiService::class.java)
    }

    fun getDocument(): Flow<Res<DocumentEntity>> {
        return apiService.getDoc()
    }

    fun deleteDocument(id: Int): Flow<Res<Any>> {
        return apiService.deleteDoc(id)
    }

    fun uploadDocument(name: String, path: String): Flow<Res<Any>> {
        //将对应路径的文件生成为一个body
        val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), File(path))
        //将body转化为一个multiBody
        val multipartBody = MultipartBody.Builder().addFormDataPart(Const.PARAM_FILE, name, body).build()
        return apiService.uploadDoc(multipartBody.parts)
    }
}